import scrapy
import json
import time
from stock_spider.items import StockItem

class EastmoneySpider(scrapy.Spider):
    # 爬虫名称
    name = 'eastmoney'
    
    # 允许的域名
    allowed_domains = ['eastmoney.com']
    
    # 起始URL（东方财富网的股票数据API）
    start_urls = [
        'http://80.push2.eastmoney.com/api/qt/clist/get?'
        'pn=1&pz=50&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&'
        'fltt=2&invt=2&fid=f3&fs=m:0+t:6,m:0+t:80,m:1+t:2,m:1+t:23&'
        'fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22'
    ]

    def parse(self, response):
        """
        解析API返回的JSON数据
        """
        try:
            # 解析JSON响应
            data = json.loads(response.text)
            stocks = data.get('data', {}).get('diff', [])
            
            # 遍历每只股票数据
            for index, stock_data in enumerate(stocks, 1):
                # 创建StockItem对象
                item = StockItem()
                
                # 填充数据
                item['id'] = index
                item['stock_code'] = stock_data.get('f12', '')  # 股票代码
                item['stock_name'] = stock_data.get('f14', '')  # 股票名称
                item['latest_price'] = stock_data.get('f2', 0)  # 最新价
                item['change_percent'] = stock_data.get('f3', 0)  # 涨跌幅%
                item['change_amount'] = stock_data.get('f4', 0)   # 涨跌额
                
                # 格式化成交量
                volume = stock_data.get('f5', 0)
                if volume >= 100000000:
                    item['volume'] = f"{volume/100000000:.2f}亿手"
                elif volume >= 10000:
                    item['volume'] = f"{volume/10000:.2f}万手"
                else:
                    item['volume'] = f"{volume}手"
                
                # 格式化成交额
                turnover = stock_data.get('f6', 0)
                if turnover >= 100000000:
                    item['turnover'] = f"{turnover/100000000:.2f}亿元"
                elif turnover >= 10000:
                    item['turnover'] = f"{turnover/10000:.2f}万元"
                else:
                    item['turnover'] = f"{turnover}元"
                
                item['amplitude'] = stock_data.get('f7', 0)  # 振幅%
                item['high'] = stock_data.get('f15', 0)  # 最高
                item['low'] = stock_data.get('f16', 0)   # 最低
                item['open_price'] = stock_data.get('f17', 0)  # 今开
                item['prev_close'] = stock_data.get('f18', 0)  # 昨收
                item['timestamp'] = time.strftime('%Y-%m-%d %H:%M:%S')  # 当前时间
                
                # 返回数据项
                yield item
                
        except Exception as e:
            self.logger.error(f"解析股票数据时出错: {e}")